<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box1{width: 300px;height: 300px;background: red}
        .box2{width: 200px;height: 200px;background: green}
        .box3{width: 100px;height: 100px;background: blue}
        span{background: pink}
        .msg{width: 100px;height: 100px;background: yellow}
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2">
            <div class="box3">
                <span>hello</span>
            </div>
        </div>
    </div>
    <div class="msg"></div>
</body>
<script>
    var obox1 = document.querySelector(".box1")
    var obox2 = document.querySelector(".box2")
    var obox3 = document.querySelector(".box3")
    var ospan = document.querySelector("span")
    var omsg = document.querySelector(".msg")


    obox1.onclick = function(){
        console.log("red")
    }
    obox2.onclick = function(eve){
        var e = eve || window.event;
        stopBubble(e);

        console.log("green")
    }
    obox3.oncontextmenu = function(){
        console.log("blue")
    }
    ospan.onclick = function(eve){
        var e = eve || window.event;

        // 阻止事件冒泡：正常浏览器
        // e.stopPropagation();
        // 阻止事件冒泡：IE浏览器
        // e.cancelBubble = true;

        stopBubble(e);

        console.log("pink")
    }
    omsg.onclick = function(){
        console.log("yellow")
    }


    // 阻止事件冒泡的兼容封装
    function stopBubble(e){
        if(e.stopPropagation){
            e.stopPropagation()
        }else{
            e.cancelBubble = true;
        }
    }

</script>
</html>